/**
 * Created by zy on 2016/6/16.
 * 主页 - pic 系统
 */


;(function($,window,document,undefined){

    // 右侧边栏展开 - 收缩效果
    $('#JQ_homepage_sidebtn').mousedown(function(){
        $(this).toggleClass('active');
        var $side = $('#JQ_homepage_side');
        var width = $side.width();
        var left = $side.css('left');
        if(left == '0px'){
            $side.stop().animate({
                left: width
            },'fast');
        }else{
            $side.stop().animate({
                left: 0
            },'fast');
        }
    });

    // 右侧边栏 TIP
    /*$('#JQ_homepage_side').delegate('a','mouseover',function(){
        var $this = $(this);
        var $tipbox = $('<div class="zy-homepage-sidebar-tip"><span></span></div>');
        $tipbox.find('span').html($this.attr('data-tip'));
        $this.append($tipbox);
        $tipbox.stop().show().stop().animate({
            'left': -40,
            'opacity': 1
        });
    });
    $('#JQ_homepage_side').delegate('a','mouseout',function(){
        var $this = $(this);
        $this.find('div.zy-homepage-sidebar-tip').remove();
    });*/

    // 本地存储中是否有 main_menu 数据
    var main_menu_storage = Dyy.getStorage('mainMenu'), main_menu_json = $.parseJSON(main_menu_storage);
    if(main_menu_json){
        fillMenu(main_menu_json.node);
    }else{
        DyyBase.ajax(DyyBase.url_main_menu(),{},function(dataset){
            fillMenu(dataset.result);
            // 将数据写入本地存储
            var str = '';
            for(var i = 0, len = dataset.result.length; i < len; i++){
                if(i == len - 1){
                    str += '{"id":"'+dataset.result[i].id+'","name":"'+dataset.result[i].name+'","url":"'+dataset.result[i].url+'"}';
                }else{
                    str += '{"id":"'+dataset.result[i].id+'","name":"'+dataset.result[i].name+'","url":"'+dataset.result[i].url+'"},';
                }
            }
            if(str.length > 0){
                str = '{"node":['+str+']}';
                Dyy.setStorage('mainMenu',str);
            }
        });
    }

    // 填充主菜单
    function fillMenu(data){
        for(var i = 0, len = data.length; i < len; i++){
            var $menu = $('a[data-menuid="'+ data[i].id +'"]');
            $menu.prop('href',DyyBase.root_path+data[i].url);
        }
        var $menus = $('a[data-status="zy-main-menu"]');
        $menus.each(function(){
            var $this = $(this);
            $this.click(function(event){
                if($this.attr('data-menuid') && !($this.prop('href'))){
                    $this.MSG({
                        content:'您没有权限进行此操作！'
                    });
                }else if($this.attr('data-menuid') == '' && !($this.prop('href'))){
                    $this.MSG({
                        content:'非常遗憾，本模块需要定制化开发，请联系销售支持！'
                    });
                }
                event.stopPropagation();
                return true;
            });
        });
    }


    /*** 加载消息列表 - start ***/
    // 每页条数
    var pagesize = 7;
    // 消息容器
    var $homepage_news = $('#JQ_homepage_news');

    // 总页数 当前页
    var $page_total = $('#JQ_msg_total');
    var $page_current = $('#JQ_msg_curent');

    // 上一页 下一页 按钮
    var $btn_prev = $('#JQ_msg_prev');
    var $btn_next = $('#JQ_msg_next');

    // 显示动态
    $homepage_news.delegate('li','mouseover',function(){
        var $this = $(this);
        var content = $this.attr('data-msg');
        $this.PL({
            content: content
        });
    });

    // 上一页
    $btn_prev.click(function(){
        var $this = $(this);
        if(!($this.hasClass('disabled'))){
            var current = parseInt($page_current.val());
            current -= 1;
            $page_current.val(current);
            getMessage(current);
        }
    });

    // 下一页
    $btn_next.click(function(){
        var $this = $(this);
        if(!($this.hasClass('disabled'))){
            var current = parseInt($page_current.val());
            current += 1;
            $page_current.val(current);
            getMessage(current);
        }
    });

    // 翻页
    function getMessage(n){

        var total = parseInt($page_total.val());

        if(n > 1){
            $btn_prev.removeClass('disabled');
        }else{
            $btn_prev.addClass('disabled');
        }

        if(total == n){
            $btn_next.addClass('disabled');
        }else{
            $btn_next.removeClass('disabled');
        }

        DyyBase.ajax(DyyBase.url_homepage_news(), {
            'jsonBean.pageCount': pagesize,
            'jsonBean.pageNum': n
        }, function (dataset) {
            if (dataset.bool) {
                var list = dataset.result.list;
                if (list.length > 0) {
                    var li = '';
                    for (var i = 0, len = list.length; i < len; i++) {
                        if (i == len - 1) {
                            li += '<li class="n-b-b" data-msg="' + list[i].msgContent + '"><span class="l w-70">● ' + list[i].msgContent + '</span><span class="r w-30 t-r f12">' + list[i].createDate + '</span></li>';
                        } else {
                            li += '<li data-msg="' + list[i].msgContent + '"><span class="l w-70">● ' + list[i].msgContent + '</span><span class="r w-30 t-r f12">' + list[i].createDate + '</span></li>';
                        }
                    }
                    $homepage_news.empty().html(li);
                } else {
                    $homepage_news.html('<p class="zy-msg-no-data-p"><img src="' + DyyBase.root_path + '/static/img/msg_no_data.png' + '"><p>');
                }
            }
        });
    }

    // 获取动态
    DyyBase.ajax(DyyBase.url_homepage_news(), {
        'jsonBean.pageCount': pagesize,
        'jsonBean.pageNum': 1
    }, function (dataset) {
        if (dataset.bool) {
            var list = dataset.result.list;
            if (list.length > 0) {

                $page_total.val(dataset.result.totalPage);
                $page_current.val(dataset.result.pageNum);

                var li = '';
                for (var i = 0, len = list.length; i < len; i++) {
                    if (i == len - 1) {
                        li += '<li class="n-b-b" data-msg="' + list[i].msgContent + '"><span class="l w-70">● ' + list[i].msgContent + '</span><span class="r w-30 t-r f12">' + list[i].createDate + '</span></li>';
                    } else {
                        li += '<li data-msg="' + list[i].msgContent + '"><span class="l w-70">● ' + list[i].msgContent + '</span><span class="r w-30 t-r f12">' + list[i].createDate + '</span></li>';
                    }
                }

                $homepage_news.empty().html(li);

                if(dataset.result.totalPage > 1){
                    $btn_next.removeClass('disabled');
                }
            } else {
                $homepage_news.html('<p class="zy-msg-no-data-p"><img src="' + DyyBase.root_path + '/static/img/msg_no_data.png' + '"><p>');
            }
        }
    });
    /*** 加载消息列表 - end ***/

})(jQuery,window,document);

/*** 动态的展示效果 - 插件 ***/
;(function($){
    var L = function(element,options){
        this.element = element;
        this.defaults = {
            width: 280,
            height: 110,
            content: '消息'
        };
        this.settings = $.extend({},this.defaults,options);
    };

    L.prototype = {
        init: function(){
            var self = this;
            var $element = $(self.element);
            var pos_t = $element.offset().top;
            var pos_l = $element.offset().left;
            var pos_w = $element.parent('ul').width();

            var $out_pl = $('<div class="zy-user-msg-box"><p class="zy-user-msg-p"></p><span class="zy-user-msg-arrow"></span></div>');
            $out_pl.css({
                'left': pos_l,
                'top': pos_t - self.settings.height,
                'width': pos_w ? pos_w : self.settings.width,
                'height': self.settings.height
            });
            $out_pl.find('p.zy-user-msg-p').html(self.settings.content);
            $(document.body).append($out_pl);

            $element.off('mouseout').mouseout(function(){
                $(document.body).find('div.zy-user-msg-box').remove();
            });
        }
    };

    $.fn.PL = function(options){
        var pl = new L(this, options);
        return pl.init();
    }
})(jQuery);